package com.debao;

import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.Interest;
import com.debao.system.service.IInterestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/follow")
public class ApiInterest {

    @Resource
    private IInterestService interestService;

    /**
     * 我的关注
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        List<Interest> interests = interestService.selectInterestList(new Interest().setUserId(SecurityUtils.getUserId()));
        return AjaxResult.success(interests);
    }

    /**
     * 关注
     */
    @PostMapping("/interest/{otherId}")
    public AjaxResult interest(@PathVariable("otherId") Long otherId) {
        int row = interestService.insertInterest(new Interest().setOtherId(otherId).setUserId(SecurityUtils.getUserId()));
        if (row == 1) {

            return AjaxResult.success("关注成功");
        }
        return AjaxResult.error("关注失败");
    }

    /**
     * 取消关注
     */
    @DeleteMapping("delInterest/{otherId}")
    public AjaxResult delInterest(@PathVariable("otherId") Long otherId) {
        int row = interestService.deleteInterest(otherId, SecurityUtils.getUserId());
        if (row == 1) {
            return AjaxResult.success("取消关注成功");
        }
        return AjaxResult.error("取消关注失败");
    }
}
